package com.example.common.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.common.R;
import com.example.common.view.WheelView;

import java.util.ArrayList;

/**
 * @author skygge
 * @date 2020/7/6.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设置时/分控件
 */
public class SetTimeDialog extends Dialog {

    private TextView mConfirm;
    private TextView mCancel;
    private WheelView mHour;
    private WheelView mMinute;
    private callBack mCallBack;

    public SetTimeDialog(@NonNull Context context, callBack call) {
        super(context, R.style.window_background2);
        this.mCallBack = call;
    }

    public void setCallBack(callBack mCallBack) {
        this.mCallBack = mCallBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_set_time);
        setCanceledOnTouchOutside(false);
        initView();
        initData();
        mCancel.setOnClickListener(v -> dismiss());
        mConfirm.setOnClickListener(v -> {
            mCallBack.showTime(mHour.getCurrentItem(), mMinute.getCurrentItem());
            dismiss();
        });
    }

    private void initData() {
        mHour.setAdapter(new NumberAdapter(getHour()));
        mMinute.setAdapter(new NumberAdapter(getMinute()));
    }

    private void initView() {
        Window window = getWindow();
        if (window!=null) {
            WindowManager.LayoutParams params = window.getAttributes();
            window.setGravity(Gravity.BOTTOM);
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            getWindow().setAttributes(params);
        }
        mConfirm = findViewById(R.id.dialog_confirm);
        mCancel  = findViewById(R.id.dialog_cancel);
        mHour = findViewById(R.id.dialog_hour);
        mMinute = findViewById(R.id.dialog_minute);
        mHour.setCyclic(true);
        mMinute.setCyclic(true);
    }

    private ArrayList<String> getHour() {
        ArrayList<String> items_hour = new ArrayList<>();
        for (int i = 0; i < 24; i ++) {
            String item = String.valueOf(i);
            if (item.length() == 1) {
                item = "0" + item;
            }
            items_hour.add(item);
        }
        return items_hour;
    }

    private ArrayList<String> getMinute() {
        ArrayList<String> items_minute = new ArrayList<>();
        for (int i = 0; i < 60; i ++) {
            String item = String.valueOf(i);
            if (item.length() == 1) {
                item = "0" + item;
            }
            items_minute.add(item);
        }
        return items_minute;
    }

    public interface callBack{
        void showTime(int hour, int minute);
    }

    private static class NumberAdapter extends WheelView.WheelArrayAdapter<String> {
        NumberAdapter(ArrayList<String> items) {
            super(items);
        }
    }
}
